<template>
  <div ref='vantaRef' v-if="hideWelcome" class="wel-box" style="width: 100%;height: 100vh">
    <div class="wel-login">
      <div class="nav-logo animate__animated animate__zoomIn">
        <div class="nav-logo-tit">
          <h4>XY校缘网</h4>
        </div>
        <div class="nav-logo-xtit">
          XIAOYUAN
        </div>
      </div>
      <div class="nav-info">
<!--        <button @click="clickLogin" class="bttn-gradient bttn-md bttn-primary">点击登录</button>-->
        <button  @click="clickLogin" class="bttn-gradient bttn-md bttn-primary">点击登录</button>
        <n-modal
            v-model:show="showModal"
            class="custom-card modal-mask"
            preset="card"
            :style="bodyStyle"
            :on-after-enter="ModelShow"
            :on-after-leave="closedModel"
            :bordered="false"
            style="width: 360px !important;"
        >
          <template #header-extra class="head-logo">
            <span style="font-size: 12px">请登录~</span>
          </template>
          <n-card size="small" class="card-cont" >
            <n-tabs
                type="line"
                animated
                default-value="oasis"
                justify-content="space-evenly"
                size="small"
            >
              <n-tab-pane name="oasis" tab="扫码登录">
                <el-image
                    :src="imgUrl"
                    fit="cover"
                    loading="lazy"
                    class="wx-ewm"/>
                <p class="wx-xtit">扫一扫,快速登录</p>
              </n-tab-pane>
            </n-tabs>
          </n-card>
        </n-modal>
<!--        <div v-show="showImg" class="img">-->
<!--          <el-image style="width: 180px;height: 180px"-->
<!--                    src="../assets/bg.jpg"-->
<!--                    fit="cover"-->
<!--                    loading="lazy">-->
<!--          </el-image>-->
<!--        </div>-->
      </div>
<!--      <div class="wel-come">-->
<!--        欢迎进入校缘网-->
<!--      </div>-->
    </div>
  </div>
</template>

<script>
// import Birds from "../vantajs/vanta.birds.min"
import CLOUDS from "../vantajs/vanta.clouds.min"
import * as THREE from "../vantajs/three.r134.min"
import {ref} from "vue";
import { useMessage } from 'naive-ui'
import {apiBase, apis, ftpUrl} from "@/global/config";
import {useRouter} from "vue-router";
export default {
  name: "Welcome",
  beforeCreate() {

  },
  created() {
    this.isClosedWebSocket = false;
    this.$requestCenter.checkWebToken().then((e) => {
          if (e.data != null) {
            this.isLogin = true
          }
    })

  },
  mounted() {
    this.vantaEffect = CLOUDS({
      el: this.$refs.vantaRef,
      THREE: THREE
    })
    // 这里重新设置样式
    this.vantaEffect.setOptions({
      mouseControls: true,
      touchControls: true,
      gyroControls: false,
      minHeight: 200.00,
      minWidth: 200.00,
      skyColor: 0x91cde3,
      cloudColor: 0xc9c9d9,
      cloudShadowColor: 0x174b7d,
      sunColor: 0xe37f05,
      speed: 1.50
    })
  },
  beforeUnmount() {
    if (this.vantaEffect) {
      this.vantaEffect.destroy()
    }
  },
  methods:{
    clickLogin(){
      if (this.isLogin){
        this.$router.push({path:"/home"})
      }else {
        this.showModal = true
        this.$requestCenter.webLogin().then((res) => {
          this.imgUrl = res.data.wxMiniCodePath;
          if (res.code == 20041) {
            this.webToken = res.data.webToken;
            this.initWebSocket();
          }
        });
      }
    },
    ModelShow() {
      this.isClosedWebSocket = false;
      this.$requestCenter.webLogin().then((res) => {
        this.imgUrl = res.data.wxMiniCodePath;
        if (res.code == 20041) {
          this.webToken = res.data.webToken;
          this.initWebSocket();
        }
      });
    },
    closedModel() {
      let result = JSON.parse(localStorage.getItem("storage"));
      console.log(result);

      if (this.websock != null) {
        this.websock.close();
      } else {
        this.isClosedWebSocket = true;
      }
      this.$requestCenter.deleteLoginWxMiniCodeImg({ imgName: this.imgUrl });
    },
    initWebSocket: function () {
      // 建立连接
      // WebSocket与普通的请求所用协议有所不同，ws等同于http，wss等同于https
      // var userId = store.getters.userInfo.id;
      if (this.webToken == -1) {
        return;
      }
      if (this.isClosedWebSocket) {
        return;
      }
      var url = apiBase + apis.webLoginSocket + this.webToken;
      url = url.replace("https://", "wss://").replace("http://", "ws://");
      this.websock = new WebSocket(url);
      this.websock.onopen = this.websocketonopen;
      // this.websock.send = this.websocketsend;
      this.websock.onerror = this.websocketonerror;
      this.websock.onmessage = this.websocketonmessage;
      this.websock.onclose = this.websocketclose;
    },
    websocketonmessage: function (e) {
      var data = eval("(" + e.data + ")");
      //处理订阅信息
      console.log("缓存");
      // localStorage.setItem("WZH-WebToken","888888888888")
      localStorage.setItem("WZH-WebToken", data.webToken);
      console.log(localStorage.getItem("WZH-WebToken"));
      // console.log(data)

      //用户头像和昵称
      console.log("用户信息", data);
      this.avatar = ftpUrl + data.user.headUrl;
      this.nickname = data.user.name;
      this.userId = data.user.id;
      this.hideHead(true)
      if (data.loginType == 1 || data.loginType == 2) {
        // this.$router.push({ path: "https://yqwzh.ink/SchoolCupid/" });
        location.href='https://yqwzh.ink/SchoolCupid/admin/index.html'
      }else {
          this.$router.push({path:"/home"})
      }
      // this.hideWelcome = false
      this.showModal = false;
      this.showLogin = false;
// this.ffff()
    },
  },
  unmounted() {
    this.hideHead(true)
  },
  setup(props,{attrs,slots,emit}){
    const router = useRouter();
    function hideHead(e){
      emit("hideHead",e)
    }
    return{
      showModal: ref(false),
      showImg:ref(false),
      bodyStyle: {
        width: "600px",
      },
      imgUrl: ref(""),
      webToken: ref(-1),
      isClosedWebSocket: ref(false),
      avatar: ref(
          "https://yqwzh.ink/SchoolCupid/defaultImg/default_headImg.png"
      ),
      hideWelcome:ref(true),
      hideHead,
      router,
      isLogin:ref(false)
    }
  }
}
</script>

<style lang="less" scoped>
.wel-box{
  overflow: hidden;
}
  .wel-login{
    //top: 100px;
    background: transparent;
    border-radius: 45px;
  }
  .card-cont {
    text-align: center;
    .login {
      text-align: center;
    }
    .wx-ewm {
      margin-top: 10px;
      width: 150px;
      height: 150px;
      border-radius: 50%;
    }
    .wx-xtit {
      padding: 10px 0;
      font-size: 12px;
      color: #bababa;
    }
  }

  //logo
  .nav-logo {
    position: relative;
    top: 100px;
    display: flex;
    flex-direction: column;
    align-items: center;
    -webkit-background-clip: text;
    color: #7aa1d2;
    .nav-logo-tit{
      h4{
        font-size: 76px;
        font-family: serif;
        font-weight: bolder;
        margin: 0;
        padding: 6px 10px;
        letter-spacing: 2px;
        text-shadow: 2px 2px #a6c2e8;
      }
    }
    .nav-logo-xtit{
      font-size: 42px;
      letter-spacing: 6px;
      font-family: serif;
      font-weight: normal;
      text-shadow: 1px 2px #dedede;
    }
  }

  //登录
  .nav-info{
    position: relative;
    top: 130px;
    text-align: center;
    .img{
      position: relative;
      left: 45%;
      width: 180px;
      height: 180px;
      border: 1px solid #eaeaea;
      margin-top: 20px;
      img{
        width: 180px;
        height: 180px;
        object-fit: cover;
      }
    }
  }

  //欢迎字体
  .wel-come{
    position: absolute;
    top: 680px;
    //margin: 0 auto;
    left: 50%;
    text-align: center;
    font-size: 12px;
    background: transparent;
    color: #fff;
  }
</style>